#coding:utf-8
from lovewith.share.models import AttachColor
from lovewith.website.views.modules.color import ColorApi


#颜色
class AttachColorApi:
    def __init__(self):
        pass

    @staticmethod
    def get_by_image(image_id):
        color_result = []
        color_data = AttachColor.objects.filter(post_image_id=int(image_id))
        for co in color_data:
            color_result.append({'id': co.color.id, 'name': co.color.name, 'value': co.color.value})
        return color_result

    @staticmethod
    def get_image_by_color(color_id):
        return AttachColor.objects.filter(post_image__share_post__is_delete=False, color_id=int(color_id))

    #删除
    @staticmethod
    def delete_by_image(image_id):
        img_color = AttachColor.objects.filter(post_image_id=image_id)
        img_color.delete()

    #添加
    @staticmethod
    def add(colors, image_data, share_data):
        new_colors = []
        for color_id in colors:
            color_data = ColorApi.check_data(color_id=color_id)
            if color_data:
                new_colors.append(
                    AttachColor(share_post=share_data, post_image=image_data, color=color_data)
                )
        AttachColor.objects.bulk_create(new_colors)































